layui.config({
    base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['form','table'], function(){
    var table = layui.table;
    var layer = layui.layer;
    var form=layui.form;
    var $ = layui.jquery;

    var ${table.lowerClassName}List= table.render({
        elem: '#${table.lowerClassName}List'
        ,height: 'full-40'
        ,limit: 15
        ,url: '/${table.lowerClassName}/listPage' //数据接口
        ,page: true //开启分页
        ,cellMinWidth: 80
        ,skin: 'row' //行边框风格
        ,even: true //开启隔行背景
        ,size: 'sm' //小尺寸的表格
        ,cols: [[ //表头
            {type:'checkbox'}
            <#list table.columns as column>
            ,{field: '${column.filedName}', title: '${column.remakes}',align: 'center'}
            </#list>
            ,{fixed: 'right', title: '操作', align:'center', toolbar: '#barDemo'}
         ]]

   });


        // 监听行工具事件
   table.on('tool(${table.lowerClassName}List)', function(obj){
       var data = obj.data; //获得当前行数据
       var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
       var tr = obj.tr; //获得当前行 tr 的DOM对象
       if(layEvent === 'update'){
           layer.open({
               type: 2
               ,area:['100%', '100%']
               ,title: "修改"
               ,btn: ['修改','取消']
               ,content: 'add.html?id='+obj.data.id
               ,yes: function(index, layero){
                   //监听提交
                   var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                   var submit=iframeWin.document.getElementById("submit");
                   submit.click();
               },
               end:function () {
                    $(".layui-laypage-btn").click();
               }
           });
       }else if(layEvent === 'showInfo'){
            layer.open({
                type: 2
                ,area:['100%', '100%']
                ,title: "查看信息"
                ,content: 'add.html?id='+obj.data.id
            });
       }else if(layEvent === 'delete'){
           layer.confirm("确定删除该信息吗？",{
               btn:['确定删除','取消']
           },function () {
               $.ajax({
                   url: "/${table.lowerClassName}/delete",
                   data:{id:data.id},
                   type:"GET",
                   success:function (result) {
                   if(result.code==0){
                       layer.msg('删除成功！', {icon: 1,time:2000,shade:0.2});
                       setInterval(function () {
                            window.location.reload();
                       },2000);
                   }
                   },
                   error:function () {
                        layer.alert("删除失败！", {icon: 2,time:2000,shade:0.2});
                   }
               })
           })
       }
   });

   //点击添加按钮
   $("#add").click(function () {
       layer.open({
           type: 2
           ,area:['100%', '100%']
           ,title: "添加信息"
           ,btn: ['添加','取消']
           ,content: 'add.html'
           ,yes: function(index, layero){
               // layer.close(index);//需要手动关闭窗口
               //监听提交
               var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
               var submit=iframeWin.document.getElementById("submit");
               submit.click();
           }
       });
   })

});